package org.zjvis.datascience.web.socket;

import com.corundumstudio.socketio.SocketConfig;
import com.corundumstudio.socketio.SocketIOServer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @description WebSocket 配置类
 * @date 2021-12-29
 */
@Configuration
public class SocketIOConfig {
    @Value("${app.socketio.port}")
    private Integer port;

    @Value("${app.socketio.boss-count}")
    private int bossCount;

    @Value("${app.socketio.work-count}")
    private int workCount;

    @Value("${app.socketio.allow-custom-requests}")
    private boolean allowCustomRequests;

    @Value("${app.socketio.upgrade-timeout}")
    private int upgradeTimeout;

    @Value("${app.socketio.ping-timeout}")
    private int pingTimeout;

    @Value("${app.socketio.ping-interval}")
    private int pingInterval;

    @Bean
    public SocketIOServer socketIOServer() throws UnknownHostException {
        SocketConfig socketConfig = new SocketConfig();
        socketConfig.setTcpNoDelay(true);
        socketConfig.setSoLinger(0);
        com.corundumstudio.socketio.Configuration config = new com.corundumstudio.socketio.Configuration();
        config.setSocketConfig(socketConfig);
        config.setHostname(InetAddress.getLocalHost().getHostAddress());
        config.setPort(port);
        config.setBossThreads(bossCount);
        config.setWorkerThreads(workCount);
        config.setAllowCustomRequests(allowCustomRequests);
        config.setUpgradeTimeout(upgradeTimeout);
        config.setPingTimeout(pingTimeout);
        config.setPingInterval(pingInterval);
        return new SocketIOServer(config);
    }

}
